'use strict';

// let Parser = require('rss-parser');
let Parser = require('../utils/rss/parser');
let parser = new Parser({timeout: 10000});

let ParserBetter = require('../utils/rss/parser');
let parserBetter = new ParserBetter({timeout: 10000});
const dayjs = require('dayjs');

const Service = require('egg').Service;

class RssSvc extends Service {


  async aifan() {
    let topics = [];

    try {
      let tab = 'yutang';

      const author_id = '6219e50a4035561f5e45a971';
      const author_name = '爱范热榜';
      const author_avatar = '/public/images/aifan.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'https://www.ifanr.com/feed';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        let pic;
        if (v['content:encoded'].includes('<img')) {
          pic = v['content:encoded'].match(/src="([^"]+)"/);
          if (pic) {
            pic = pic[1] + '?imageView2/2/w/500/interlace/1';
          }
        }

        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tags: v.categories,
          pic,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('爱范热榜', e);
    } finally {
      return topics;
    }
  }


  async chulewang() {
    let topics = [];

    try {
      let tab = 'yutang';

      const author_id = '628a31682880892375658a0c';
      const author_name = '触乐网';
      const author_avatar = '/public/images/chulewang.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'http://www.chuapp.com/feed';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        let pic;
        if (v.content.includes('<img')) {
          pic = v.content.match(/src="([^"]+)"/);
          if (pic) {
            pic = pic[1];
          }
        }

        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tags: v.categories,
          pic,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('触乐网', e);
    } finally {
      return topics;
    }
  }


  async dongdiankeji() {
    let topics = [];

    try {
      let tab = 'yutang';

      const author_id = '627cdd6ccf8fb8d321790be0';
      const author_name = '动点科技';
      const author_avatar = '/public/images/dongdiankeji.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'https://cn.technode.com/feed/';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        let pic;
        if (v['content:encoded'].includes('<img')) {
          pic = v['content:encoded'].match(/src="([^"]+)"/);
          if (pic) {
            pic = pic[1];
          }
        }

        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50).replace(' […]', '...'),
          tags: v.categories,
          pic,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('动点科技', e);
    } finally {
      return topics;
    }
  }


  async jihewang() {
    let topics = [];

    try {
      let tab = 'yutang';

      const author_id = '6219d40494c2c31cc78062c9';
      const author_name = '机核网';
      const author_avatar = '/public/images/jihewang.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'https://www.gcores.com/rss';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        let pic;
        if (v.content.includes('<img')) {
          pic = v.content.match(/src="([^"]+)"/);
          if (pic) {
            pic = pic[1];
          }
        }

        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tags: v.categories,
          pic,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('机核网', e);
    } finally {
      return topics;
    }
  }


  async lieyunwang() {
    let topics = [];

    try {
      let tab = 'yutang';

      const author_id = '627cc36bb6bd69c94492ed1a';
      const author_name = '猎云网';
      const author_avatar = '/public/images/lieyunwang.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'https://www.lieyunwang.com/newrss/feed.xml';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        let pic;
        if (v['content:encoded'].includes('<img')) {
          pic = v['content:encoded'].match(/src="([^"]+)"/);
          if (pic) {
            pic = pic[1] + '?imageView2/2/w/500/interlace/1';
          }
        }

        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tags: v.categories,
          pic,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('猎云网', e);
    } finally {
      return topics;
    }
  }


  async meituan() {
    let topics = [];

    try {
      let tab = 'tech';
      let sub_tab = '大厂团队';

      const author_id = '628ada0bb3ed682a5d9ab72e';
      const author_name = '美团技术';
      const author_avatar = 'https://taosha01-1253585015.cos.ap-shanghai.myqcloud.com/typora/meituanjishu.jpeg';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'https://tech.meituan.com/feed/';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];

        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tags: v.categories,
          tab,
          sub_tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('美团技术', e);
    } finally {
      return topics;
    }
  }


  async ruanyifeng() {
    let topics = [];
    try {
      let tab = 'tech';
      let boards = ['其他'];

      const author_id = '627b840023d14e914d2d6e87';
      const author_name = '阮一峰的博客';
      const author_avatar = '/public/images/ruanyifeng.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'http://www.ruanyifeng.com/blog/atom.xml';
      let feed = await parser.parseURL(url);

      let items = feed.items;
      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        // console.log(v);
        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tab,
          boards,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('阮一峰的博客', e);
    } finally {
      return topics;
    }
  }


  async shuziweiba() {
    let topics = [];

    try {
      let tab = 'yutang';

      const author_id = '6219e595340c9d1fc6ea9912';
      const author_name = '数字尾巴';
      const author_avatar = '/public/images/shuziweiba.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'https://www.dgtle.com/rss/dgtle.xml';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        let pic;
        if (v['content'].includes('<img')) {
          pic = v['content'].match(/src="([^"]+)"/);
          if (pic) {
            pic = pic[1];
          }
        }

        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tags: v.categories,
          pic,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('数字尾巴', e);
    } finally {
      return topics;
    }
  }


  async taimeiti() {
    let topics = [];

    try {
      let tab = 'yutang';

      const author_id = '6219d1e5c8b2fc1c6a9d6e2d';
      const author_name = '钛媒体';
      const author_avatar = '/public/images/taimeiti.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'http://www.tmtpost.com/feed';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        let pic;
        if (v['content:encoded'].includes('<img')) {
          pic = v['content:encoded'].match(/src="([^"]+)"/);
          if (pic) {
            pic = pic[1] + '?imageView2/2/w/500/interlace/1';
          }
        }

        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tags: v.categories,
          pic,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('钛媒体', e);
    } finally {
      return topics;
    }
  }


  async w2solo() {
    let topics = [];

    try {
      let tab = 'tech';

      const author_id = '627ca11bdd2545c05cb0b0d5';
      const author_name = '独立开发者社区';
      const author_avatar = '/public/images/w2solo.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'https://w2solo.com/topics/feed';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        // console.log(v);
        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tags: v.categories,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('独立开发者社区', e);
    } finally {
      return topics;
    }
  }

  async yixieshi() {
    let topics = [];

    try {
      let tab = 'yutang';

      const author_id = '627d13433f1f1869cc3c285c';
      const author_name = '互联网的一些事';
      const author_avatar = '/public/images/yixieshi.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'https://feed.yixieshi.com';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        // console.log(v);
        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tags: v.categories,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('互联网的一些事', e);
    } finally {
      return topics;
    }
  }


  async youquhome() {
    return [];
    let topics = [];

    try {
      let tab = 'yutang';

      const author_id = '627db23b3023ccdde145078f';
      const author_name = '有趣网址之家';
      const author_avatar = '/public/images/youquhome.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'https://youquhome.com/feed/';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        let pic;
        if (v['content:encoded'].includes('<img')) {
          pic = v['content:encoded'].match(/src="([^"]+)"/);
          if (pic) {
            pic = pic[1];
          }
        }

        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tags: v.categories,
          pic,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('有趣网址之家', e);
    } finally {
      return topics;
    }
  }


  async youshewang() {
    let topics = [];

    try {
      let tab = 'yutang';

      const author_id = '628a34f72144a125c7eb8bfb';
      const author_name = '优设网';
      const author_avatar = '/public/images/youshe.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";

      let url = 'http://www.uisdc.com/feed';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        let pic;
        if (v['content:encoded'].includes('<img')) {
          pic = v['content:encoded'].match(/src="([^"]+)"/);
          if (pic) {
            pic = pic[1];
          }
        }

        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          intro: v.summary || v.contentSnippet.replace(/\n/g, ' ').substr(0, 50) + '...',
          tags: v.categories,
          pic,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('优设网', e);
    } finally {
      return topics;
    }
  }


  async zhihu() {
    let topics = [];

    try {
      let tab = 'yutang';

      const author_id = '620899babc0e87448a243aad';
      const author_name = '知乎推荐';
      const author_avatar = '/public/images/zhihu.png';
      const source = author_name + dayjs().format('YYYY-MM-DD') + ":rss";
      const boards = ['问答'];
      const tags = [];

      let url = 'https://www.zhihu.com/rss';
      let feed = await parser.parseURL(url);

      let items = feed.items;

      for (let i = 0; i < items.length; i++) {
        let v = items[i];
        let pic;
        if (v.content.includes('<img')) {
          pic = v.content.match(/src="([^"]+)"/);
          if (pic) {
            pic = pic[1];
          }
        }

        let topic = {
          title: v.title,
          content: v.link,
          original_url: v.link,
          tags,
          boards,
          tab,
          author_id,
          author_name,
          author_avatar,
          source,
        };
        topics.push(topic);
      }
    } catch (e) {
      this.logger.error('知乎推荐', e);
    } finally {
      return topics;
    }
  }


}

module.exports = RssSvc;
